package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
    * 查看购物车
    * */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
    * 添加购物车
    * */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //存在 数量+1
        if(list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1); // updata shopping_cart set number = ? where id = ?
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //不存在 插入一条用户的数据
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null) {
                //本次添加到购物车的是菜品
                Dish byId = dishMapper.getById(dishId);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }else {
                //本次添加到购物车的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal bySetmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(bySetmeal.getName());
                shoppingCart.setImage(bySetmeal.getImage());
                shoppingCart.setAmount(bySetmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);


        }

    }

    /**
    * 清空购物车
    * */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        ShoppingCart shoppingCart = new ShoppingCart();
        if(dishId != null)
        {
            shoppingCart.setDishId(dishId);
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            if(list != null && list.size() > 0)
            {
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()-1);
                if(cart.getNumber()== 0)
                {
                    shoppingCartMapper.deleteById(cart.getId());
                }else {
                    shoppingCartMapper.updateNumberById(cart);
                }
            }
        }else
        {
            Long setmealId = shoppingCartDTO.getSetmealId();
            shoppingCart.setSetmealId(setmealId);
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            if(list != null && list.size() > 0)
            {
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()-1);
                if(cart.getNumber()== 0)
                {
                    shoppingCartMapper.deleteById(cart.getId());
                }else {
                    shoppingCartMapper.updateNumberById(cart);
                }
            }
        }
    }
}
